<?php
/*
 * @Author: your name
 * @Date: 2020-10-14 10:20:53
 * @LastEditTime: 2020-12-28 14:07:59
 * @LastEditors: Please set LastEditors
 * @Description: 访问数据学习功能管理端接口
 * @FilePath: /special/application/adminapiv3/controller/module_manage/netfilter/AccessLogLearnController.php
 */
namespace app\adminapiv3\controller\module_manage\netfilter;

use app\adminapiv3\service\module_manage\netfilter\AccessLogLearnService;
use app\common\service\File;
use think\facade\Request;

class AccessLogLearnController extends NetfilterBaseController
{
    /**
     * @name: 学习设置详情
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-31 10:30:06
     */
    public function configInfo ()
    {
        $input = Request::only(['host_id'], 'post');
        $this->validate($input, 'AccessLogLearn.oneHostId');
        $AccessLogLearnService = new AccessLogLearnService();
        $data = $AccessLogLearnService->configInfo($input['host_id']);
        return $this->jsonOutPut($data);
    }

    /**
     * @name: 学习设置修改
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-31 10:30:06
     */
    public function configModify (\think\Request $request)
    {
        if(!File::lock('configModifyApiLock:'.$request->user_id,1)){
            $this->setError($this->CODE_FAIL,'请勿频繁点击');
            return $this->jsonOutPut([],false);
        }

        $input = Request::only([
            'host_id','id','keep_time','auto','model','allow','now','have_mac_addr','danger_port' => '',
        ], 'post');
        $this->validate($input, 'AccessLogLearn.modify');
        $AccessLogLearnService = new AccessLogLearnService();
        $AccessLogLearnService->configModify($input['host_id'], $input);
        //如果是立即学习,则下发命令至客户端
        $input['now'] ? $this->sendCommandToClient('netfilter', 'study') : '';
        return $this->jsonOutPut([], true);
    }

    /**
     * @name: 停止学习
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-31 11:06:35
     */
    public function learnOff ()
    {
        $input = Request::only(['host_id']);
        $this->validate($input, 'AccessLogLearn.bulkHostId');
        $AccessLogLearnService = new AccessLogLearnService();
        $AccessLogLearnService->learnOff($input['host_id']);
        //下发命令至客户端
        $this->sendCommandToClient('netfilter', 'study');
        return $this->jsonOutPut([], true);
    }

    /**
     * @name: 批量学习
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-10-31 11:06:35
     */
    public function bulkLearnOn (\think\Request $request)
    {
        if(!File::lock('bulkLearnOnApiLock:'.$request->user_id,1)){
            $this->setError($this->CODE_FAIL,'请勿频繁点击');
            return $this->jsonOutPut([],false);
        }
        $input = Request::only([
            'host_id','keep_time','auto','model','allow','now','have_mac_addr','danger_port' => ''
        ], 'post');

        $this->validate($input, 'AccessLogLearn.bulkLearnOn');
        unset($input['host_id']);
        $AccessLogLearnService = new AccessLogLearnService();
        $AccessLogLearnService->bulklearnOn($input);

        //如果是立即学习,则下发命令至客户端
        $input['now'] ? $this->sendCommandToClient('netfilter', 'study') : '';

        return $this->jsonOutPut([],true);
    }

    /**
     * @name: 单个学习
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-10-16 14:53:59
     */
    public function oneLearnOn(\think\Request $request)
    {
        if(!File::lock('oneLearnOnApiLock:'.$request->user_id,1)){
            $this->setError($this->CODE_FAIL,'请勿频繁点击');
            return $this->jsonOutPut([],false);
        }
        $input = Request::only(['host_id'], 'post');
        $this->validate($input, 'AccessLogLearn.oneLearnOn');
        $AccessLogLearnService = new AccessLogLearnService();
        $AccessLogLearnService->oneLearnOn();

        //下发命令
        $this->sendCommandToClient('netfilter', 'study');
        return $this->jsonOutPut([],true);
    }

    /**
     * @name: 学习数据组列表
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-01 16:21:17
     */
    public function LearnDataStatisticsList ()
    {
        $input = Request::only(['protocol','src_ip','dest_ip','dest_port','ctime','direction','host_id'], 'post');
        $this->validate($input, 'AccessLogLearn.dataStatisticsFilter');
        $AccessLogLearnService = new AccessLogLearnService();
        $data = $AccessLogLearnService->LearnDataStatisticsList($input);
        return $this->jsonOutPut($data);
    }

    /**
     * @name: 学习数据
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-01 17:36:19
     */
    public function learnDataList ()
    {
        $input = Request::only([
            'protocols','src_ip','dest_ip','dest_port','ctime','direction','status','host_id','mark' => '','icon' => ''
        ], 'post');
        $this->validate($input, 'AccessLogLearn.dataListFilter');
        $AccessLogLearnService = new AccessLogLearnService();
        $data = $AccessLogLearnService->LearnDataList($input);

        return $this->jsonOutPut($data);
    }

    /**
     * @name: 删除根据学习数据生成的规则
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-04 14:09:34
     */
    public function learnDataDelete ()
    {
        $input = Request::only(['learn_id','host_id'], 'post');
        $this->validate($input, 'AccessLogLearn.handleLearnData');

        $AccessLogLearnService = new AccessLogLearnService();
        $data = $AccessLogLearnService->learnDataDelete($input['host_id'], $input['learn_id']);

        //下发命令 查询规则列表
        $this->sendCommandToClient('netfilter','downloadRules');
        return $this->jsonOutPut([],true);
    }

    /**
     * @name: 自学习规则创建规则
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2019-11-04 16:57:01
     */
    public function createFirewallRule ()
    {
        $input = Request::only(['learn_id','host_id'], 'post');
        $this->validate($input, 'AccessLogLearn.handleLearnData');

        $AccessLogLearnService = new AccessLogLearnService();
        $data = $AccessLogLearnService->createFirewallRule($input['host_id'], $input['learn_id']);

        //下发命令 查询规则列表
        $this->sendCommandToClient('netfilter','downloadRules');
        return $this->jsonOutPut([],true);
    }
}